// ITI 1120 Fall 2006, Lab 8, Example 2 // Name: Diana Inkpen, Student# 123456 /** * PROGRAM: to check to see if a given array is in sorted order. */ class ArraySorted { public static void main (String[] args) { // DECLARE VARIABLES/DATA DICTIONARY int[] a; // GIVEN: an array of integers int n; // GIVEN: the size of the array boolean result; // RESULT: true if the array is sorted // PRINT OUT IDENTIFICATION INFORMATION System.out.println(); System.out.println("ITI 1120 Fall 2005, Lab 9, Example 2"); System.out.println("Name: Diana Inkpen, Student# 123456"); System.out.println(); // READ IN GIVENS System.out.println("Please enter a set of integers on one line separated by spaces"); a = ITI1220.readIntLine(); n = a.length; // BODY OF ALGORITHM result = checkSorted( a, n ); // PRINT OUT RESULTS AND MODIFIEDS if (result) { System.out.println("The array is sorted"); } else { System.out.println("The array is not sorted"); } } // If the 'main' method calls other algorithms, put the method(s) below. /** * METHOD: Recursive method 'checkSorted' which checks to see if an * array is in sorted order or not. * * GIVENS: a: an array of integers * n: the length of the array */ public static boolean checkSorted( int[] a, int n ) { // DECLARE VARIABLES/DATA DICTIONARY boolean sorted; // RESULT: true if the array is sorted // BODY OF ALGORITHM if( a[n-2] < a[n-1] ) { if ( n == 2 ) { sorted = true; } else { sorted = checkSorted( a, n-1 ); } } else { sorted = false; } // RETURN RESULT return sorted; } }